package com.shaoyu.elasticsearch.base;


import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Description:
 * @Author: shaoyu1
 * @Date Create in 2018/8/2 15:20
 */
public class BaseDataType implements Serializable {

    public boolean containsProperty(String filedname){
        try{
            return this.getClass().getDeclaredField(filedname)!=null;
        }catch (NoSuchFieldException e){
            e.printStackTrace();
            return false;
        }
    }
    public Object get(String filedName,Object def) throws  InvocationTargetException, IllegalAccessException {
        if(containsProperty(filedName)){
            return get(filedName)==null?def:get(filedName);
        }else {
            return def;
        }
    }
    public Object get(String filedName) throws  InvocationTargetException, IllegalAccessException {
        if(!containsProperty(filedName)){
            return null;
        }else{
            try{
                return getPropertyGetMethod(this.getClass(),filedName).invoke(this);
            }catch (NullPointerException  e){
                throw new RuntimeException("属性"+filedName+"没有对应的get方法");
            }
        }
    }
    public BaseDataType set(String filedName,Object property)throws  InvocationTargetException, IllegalAccessException {
        if(!containsProperty(filedName)){
            throw new RuntimeException("没有对应的属性:"+filedName);
        }else {
            try{
                getPropertySetMethod(this.getClass(),filedName).invoke(this,property);
            }catch (NullPointerException e){
                throw new RuntimeException("属性"+filedName+"没有对应的set方法");
            }
        }
        return this;
    }

    private Method getPropertyGetMethod(Class<?> type, String filedName) {
        Method[] methods = type.getMethods();
        for (Method method : methods) {
            if (method.getParameterTypes().length != 0)
                continue;

            if (method.getName().equalsIgnoreCase("get" + filedName)) {
                return method;
            }

            if ((Boolean.class.isAssignableFrom(method.getReturnType()) || "boolean".equals(method.getReturnType().getName())) && method.getName().equalsIgnoreCase("is" + filedName)) {
                return method;
            }
        }
        return null;
    }
    private Method getPropertySetMethod(Class<?> type, String filedName) {
        Method[] methods = type.getMethods();
        for (Method method : methods) {
            if (method.getParameterTypes().length != 1)
                continue;
            if (method.getName().equalsIgnoreCase("set" + filedName)) {
                return method;
            }
            if ((Boolean.class.isAssignableFrom(method.getParameterTypes()[0]) || "boolean".equals(method.getParameterTypes()[0].getName())) && method.getName().equalsIgnoreCase("set" + filedName.replace("is", ""))) {
                return method;
            }
        }
        return null;
    }
}
